// packageActivitys/ndzdDetail/ndzdDetail.js
import {
	getqueryuserreport
} from '../api/api'
import user from '../../utils/user.js'
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		current: 0,
		audioCtx: null,
		open: true,
		msg: null,
		issuperadmin: false,
		reqend: false,
		timer: null,
		// animationData: {},
		percent: 0,
		audioCtxInner: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		wx.loadFontFace({
			family: 'Heavy',
			source: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file-font/Heavy.subset.ttf',
			success: function (e) {
				console.log('success', e);
			},
			error: function (e) {
				console.log('error', e);
			},
		})
		// 获取设备宽度定位slider
		const info = wx.getSystemInfo()
		// 创建动画的方法
		const animation = wx.createAnimation({
			duration: 1000,
			timingFunction: 'ease',
		})
		// 按步骤执行动画 1. 让滑块移动到大约40%的位置
		animation.translate((info.windowWidth - 60) * 0.4).step()
		//this.animationData = animation.export()
		//this.percent = 44
		this.setData({
			animationData: animation.export(),
			percent: 44
		})
		// 延时2秒 执行动画2. 让滑块移动到大约100%的位置
		setTimeout(function () {
			animation.translate(info.windowWidth - 80).step()
			//this.animationData = animation.export()
			//this.percent = 100
			this.setData({
				animationData: animation.export(),
				percent: 100
			})
		}.bind(this), 2000)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
        wx.setStorageSync('hasRead2023ndzd', 'true')
		this.setData({
			reqend: false
		})
		if (!this.data.audioCtx) {
			this.data.audioCtx = wx.createInnerAudioContext()
			this.data.audioCtx.src = 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic1.mp3'
			this.data.audioCtx.autoplay = true
			this.data.audioCtx.loop = true
			this.data.audioCtx.play()
			this.setData({
				audioCtx: this.data.audioCtx
			})
		}
		user.waitLogin().then(async () => {
			this.setPrecetage()
			await getqueryuserreport().then((res) => {
				console.log(res);
				if (res.latestWorkingHour) {
					res.latestWorkingHourarr = res.latestWorkingHour.split('')
				}
				if (res.dailyInvitationAverage <= 1) {
					res.dailyInvitationAveragec = Math.floor(res.dutyDays / res.invitationCount)
				} else {
					res.dailyInvitationAverage = Math.ceil(res.dailyInvitationAverage)
				}
				if (res.dailySalaryPaymentAverage <= 1) {
					res.dailySalaryPaymentAveragec = Math.floor(res.dutyDays / res.salaryPaymentCount)
				} else {
					res.dailySalaryPaymentAverage = Math.ceil(res.dailySalaryPaymentAverage)
				}
				if (res.dailySalaryPaymentAverage <= 1) {
					res.averageApprovalCountec = Math.floor(res.dutyDays / res.approvalCount)
				} else {
					res.averageApprovalCount = Math.ceil(res.averageApprovalCount)
				}
				clearInterval(this.data.timer)

				this.setData({
					msg: res,
					reqend: true,
					issuperadmin: user.isSuperAdmin,
					percent: 100
				})

				this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic3.MP3')
			})
		})
		// getqueryuserreport().then((res) => {
		//     console.log(res.joinTeamDays);
		//     this.setData({
		//         msg: res,
		//         issuperadmin:user.isSuperAdmin
		//     })
		// })
	},
	playMusic(src) {
		if (this.data.audioCtxInner) {
			this.data.audioCtxInner.stop()
		}
		if(!src) {
			return
		}
		setTimeout(() => {
			this.data.audioCtxInner = wx.createInnerAudioContext()
			this.data.audioCtxInner.src = src
			this.data.audioCtxInner.autoplay = true
			this.data.audioCtxInner.play()
		}, 500);
	}, 
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {
		this.data.audioCtx.pause()
	},

	onUnload() {
		this.data.audioCtx.stop()
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */


	chnageswiper(e) {
		console.log(9999, e);
		const current = e.detail.current
		if (this.data.msg.isOpenPaySalary) {
			switch (current) {
				case 0:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic3.MP3')
					break
				case 1:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic5.MP3')
					break
				case 2:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic6.MP3')
					break
				case 3:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic4.MP3')
					break
				case 4:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic2.MP3')
					break
				case 5:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic8.MP3')
					break
				case 6:
					this.playMusic('')
					break
			}
		} else {
			switch (current) {
				case 0:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic3.MP3')
					break
				case 1:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic6.MP3')
					break
				case 2:
					this.playMusic('https://bullet001.oss-cn-shanghai.aliyuncs.com/file-music/activeBackgroundMusic4.MP3')
					break
				case 3:
					this.playMusic('')
					break
			}
		}
		this.setData({
			current: e.detail.current
		})
	},

	share2023() {
		console.log('分享2023');
		wx.navigateTo({
			url: '/packageActivitys/inviteFriends/inviteFriends',
		})
	},
	gotoreturnmoney() {
		wx.navigateTo({
			url: '/packageActivity/pages/annually-review/annually-review',
		})
		console.log('去返现');
	},
	yinpinkg() {
		this.setData({
			open: !this.data.open
		})
		if (this.data.open) {
			this.data.audioCtx.play()
		} else {
			this.data.audioCtx.pause()
		}
	},
	onUnload() {
		this.data.audioCtx.pause()
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	},

	setPrecetage() {
		this.data.timer = setInterval(() => {
			this.data.percent++
			if (this.data.percent >= 99 && !this.data.reqend) {
				this.data.percent = 99
				clearInterval(this.data.timer)
			}
			this.setData({
				percent: this.data.percent
			})
		}, 100)
	}
})